﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Meebey.SmartIrc4net;

namespace BattleMaster_IRCbot
{
    class IRChandler
    {
        IrcClient irc;
        Game gameEnvironment;
        private string server;
        private int port;
        private string channel;
        private string botNick;

        public IRChandler(IrcClient passedIRC, Game passedGame)
        {
            irc = passedIRC;
            gameEnvironment = passedGame;

            // Set IRC veriables
            // Temporarily arbitrary connection variables.
            server = "irc.furnet.org";
            port = 6667;
            channel = "#mewtwo";
            botNick = "KazBot";

            irc.Encoding = System.Text.Encoding.UTF8;
            irc.SendDelay = 200;
            irc.ActiveChannelSyncing = true;

            irc.OnError += new Meebey.SmartIrc4net.ErrorEventHandler(OnError);
            irc.OnChannelMessage += new IrcEventHandler(OnChannelMessage);
            irc.OnConnecting += new EventHandler(OnConnecting);
            irc.OnConnected += new EventHandler(OnConnected);
            irc.OnJoin += new JoinEventHandler(OnJoin);
            irc.OnQueryMessage += new IrcEventHandler(OnQueryMessage);
            irc.OnQueryNotice += new IrcEventHandler(OnQueryNotice);
            irc.OnQuit += new QuitEventHandler(OnQuit);
            irc.OnWriteLine += new WriteLineEventHandler(OnWriteLine);

            try
            {
                irc.Connect(server, port);
                // here we logon and register our nickname and so on 
                irc.Login(botNick, "Battlemaster RPG Bot");
                // join the channel
                irc.RfcJoin(channel);
            }
            catch (ConnectionException e) { Program.Exit(); }
            catch (Exception e)
            {
                System.Console.WriteLine("Error occurred! Message: " + e.Message);
                System.Console.WriteLine("Exception: " + e.StackTrace);
                Program.Exit();
            }
        }

        public void OnChannelMessage(object sender, IrcEventArgs e)
        {
            Console.WriteLine("(OnReadLine): {0}: {1}", e.Data.Nick, e.Data.Message);
            if (e.Data.Message.StartsWith("*/"))
            {
                string msgOrigen = e.Data.Nick;
                string input = e.Data.Message.Remove(0, 2);
                gameEnvironment.ProcessInput(msgOrigen, input);
            }
        }

        public void OnConnecting(object sender, EventArgs e)
        {
            System.Console.WriteLine("Connecting to server...");
        }

        public void OnConnected(object sender, EventArgs e)
        {
            System.Console.WriteLine("Connected.");
        }

        public void OnError(object sender, Meebey.SmartIrc4net.ErrorEventArgs e)
        {
            System.Console.WriteLine("Error: " + e.ErrorMessage);
            Program.Exit();
        }

        public void OnJoin(object sender, JoinEventArgs e)
        {
            if (e.Who != botNick)
            {
                Console.WriteLine(String.Format("{0} has joined {1}.", e.Who, channel));
                gameEnvironment.makeUserFile(e.Who);
            }
        }

        public void OnQueryMessage(object sender, IrcEventArgs e)
        {
            string msgOrigen = e.Data.Nick;
            Console.WriteLine("(OnQuerryMessage): {0}: {1}", msgOrigen, e.Data.Message);
            gameEnvironment.ProcessInput(msgOrigen, e.Data.Message);
        }

        public void OnQueryNotice(object sender, IrcEventArgs e)
        {
            string msgOrigen = e.Data.Nick;
            if (msgOrigen == null) msgOrigen = "System";
            Console.WriteLine("(OnQuerryNotice): {0}: {1}", msgOrigen, e.Data.Message);
            gameEnvironment.ProcessInput(msgOrigen, e.Data.Message);
        }

        public void OnQuit(object sender, QuitEventArgs e)
        {
            foreach (Character C in gameEnvironment.Characters)
            {
                if (C.Name == e.Data.Nick)
                {
                    gameEnvironment.removeCharacter(e.Data.Nick);
                }
            }
        }

        public void OnWriteLine(object sender, WriteLineEventArgs e)
        {
            string[] msg = e.Line.Split(' ');
            if (msg[0] == "NOTICE" || msg[0] == "PRIVMSG") System.Console.WriteLine("(sent) {0}", e.Line);
        }

        public void Broadcast(string msg)
        {
            SendType msgType = SendType.Message;
            irc.SendMessage(msgType, channel, msg);
        }

        public void sendNotice(string destination, string msg)
        {
            irc.RfcNotice(destination, msg);
        }

        public void sendPRIVMSG(string destination, string msg)
        {
            irc.RfcPrivmsg(destination, msg);
        }
    }
}
